package mirkofrancuski.battleship.gui;

import mirkofrancuski.battleship.R;
import mirkofrancuski.battleship.game.Cell;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class ScoreBoard extends View {

	private int mCellWidth;

	private Bitmap mHumanIcon;
	private Bitmap mHumanShip5, mHumanShip4, mHumanShip3, mHumanShip2;
	private Bitmap mHumanSunkenShip5, mHumanSunkenShip4, mHumanSunkenShip3,
			mHumanSunkenShip2;

	private Bitmap mComputerIcon;
	private Bitmap mComputerShip5, mComputerShip4, mComputerShip3,
			mComputerShip2;
	private Bitmap mComputerSunkenShip5, mComputerSunkenShip4,
			mComputerSunkenShip3, mComputerSunkenShip2;

	public ScoreBoard(Context context) {
		this(context, null);
	}

	public ScoreBoard(Context context, AttributeSet attrs) {
		super(context, attrs);

		if (!isInEditMode()) {

			WindowManager wm = (WindowManager) getContext().getSystemService(
					Context.WINDOW_SERVICE);
			Display display = wm.getDefaultDisplay();
			int gameBoardSize = display.getWidth();

			mCellWidth = gameBoardSize / 12;

		} else
			mCellWidth = this.getWidth() / 12;

		//View.inflate(context, R.layout.score_board, this);
		
		loadBitmaps();
		//setBitmaps();

	}

	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(mCellWidth * 12, mCellWidth * 16);
	}

	public void sunkHumanShip(Cell ship) {
		switch (ship) {
		case AIRCRAFT_CARRIER:
			((ImageView) findViewById(R.id.human_ship5))
					.setImageBitmap(mHumanSunkenShip5);
			break;
		case BATTLESHIP:
			((ImageView) findViewById(R.id.human_ship4))
					.setImageBitmap(mHumanSunkenShip4);
			break;
		case SUBMARINE:
			((ImageView) findViewById(R.id.human_ship3))
					.setImageBitmap(mHumanSunkenShip3);
			break;
		case CRUISER:
			((ImageView) findViewById(R.id.human_ship3c))
					.setImageBitmap(mHumanSunkenShip3);
			break;
		case DESTROYER:
			((ImageView) findViewById(R.id.human_ship2))
					.setImageBitmap(mHumanSunkenShip2);
			break;

		default:
			break;
		}
	}

	public void increaseHumanShots() {
		TextView counter = (TextView) findViewById(R.id.human_shots_number);
		int shots = Integer.parseInt(counter.getText().toString());
		shots++;
		counter.setText(Integer.toString(shots));
	}

	private void loadBitmaps() {
		Resources res = this.getResources();

		mHumanIcon = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.user_icon),
				mCellWidth * 2, mCellWidth * 2, true);

		mHumanShip5 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship5_left_half),
				mCellWidth * 5 / 2, mCellWidth, true);

		mHumanSunkenShip5 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship5_sunken_left_half),
				mCellWidth * 5 / 2, mCellWidth, true);

		mHumanShip4 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship4_left_half),
				mCellWidth * 4 / 2, mCellWidth, true);

		mHumanSunkenShip4 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship4_sunken_left_half),
				mCellWidth * 4 / 2, mCellWidth, true);

		mHumanShip3 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship3_left_half),
				mCellWidth * 3 / 2, mCellWidth, true);

		mHumanSunkenShip3 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship3_sunken_left_half),
				mCellWidth * 3 / 2, mCellWidth, true);

		mHumanShip2 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship2_left_half),
				mCellWidth * 2 / 2, mCellWidth, true);

		mHumanSunkenShip2 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship2_sunken_left_half),
				mCellWidth * 2 / 2, mCellWidth, true);

		mComputerIcon = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.computer_icon),
				mCellWidth * 2, mCellWidth * 2, true);

		mComputerShip5 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship5_right_half),
				mCellWidth * 5 / 2, mCellWidth, true);

		mComputerSunkenShip5 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship5_sunken_right_half),
				mCellWidth * 5 / 2, mCellWidth, true);

		mComputerShip4 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship4_right_half),
				mCellWidth * 4 / 2, mCellWidth, true);

		mComputerSunkenShip4 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship4_sunken_right_half),
				mCellWidth * 4 / 2, mCellWidth, true);

		mComputerShip3 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship3_right_half),
				mCellWidth * 3 / 2, mCellWidth, true);

		mComputerSunkenShip3 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship3_sunken_right_half),
				mCellWidth * 3 / 2, mCellWidth, true);

		mComputerShip2 = Bitmap.createScaledBitmap(
				BitmapFactory.decodeResource(res, R.drawable.ship2_right_half),
				mCellWidth * 2 / 2, mCellWidth, true);

		mComputerSunkenShip2 = Bitmap.createScaledBitmap(BitmapFactory
				.decodeResource(res, R.drawable.ship2_sunken_right_half),
				mCellWidth * 2 / 2, mCellWidth, true);
	}

	public void setBitmaps() {
		ImageView imageView = null;

		imageView = (ImageView) findViewById(R.id.human_icon);
		imageView.setImageBitmap(mHumanIcon);
		imageView = (ImageView) findViewById(R.id.human_ship5);
		imageView.setImageBitmap(mHumanShip5);
		imageView = (ImageView) findViewById(R.id.human_ship4);
		imageView.setImageBitmap(mHumanShip4);
		imageView = (ImageView) findViewById(R.id.human_ship3);
		imageView.setImageBitmap(mHumanShip3);
		imageView = (ImageView) findViewById(R.id.human_ship3c);
		imageView.setImageBitmap(mHumanShip3);
		imageView = (ImageView) findViewById(R.id.human_ship2);
		imageView.setImageBitmap(mHumanShip2);

		imageView = (ImageView) findViewById(R.id.computer_icon);
		imageView.setImageBitmap(mComputerIcon);
		imageView = (ImageView) findViewById(R.id.computer_ship5);
		imageView.setImageBitmap(mComputerShip5);
		imageView = (ImageView) findViewById(R.id.computer_ship4);
		imageView.setImageBitmap(mComputerShip4);
		imageView = (ImageView) findViewById(R.id.computer_ship3);
		imageView.setImageBitmap(mComputerShip3);
		imageView = (ImageView) findViewById(R.id.computer_ship3c);
		imageView.setImageBitmap(mComputerShip3);
		imageView = (ImageView) findViewById(R.id.computer_ship2);
		imageView.setImageBitmap(mComputerShip2);

	}
}
